import { createRouter, createWebHistory } from 'vue-router'

// 导入特点页面
import Features from '@/views/Features.vue';
import VocabularyChallenge from '@/views/VocabularyChallenge.vue';

// 定义基础路由
const baseRoutes = [
  {
    path: '/',
    name: 'Landing',
    component: () => import('../views/Landing.vue')
  },
  {
    path: '/features',
    name: 'Features',
    component: Features,
    meta: {
      title: '特点 - FrontierX',
      requiresAuth: false
    }
  },
  {
    path: '/pricing',
    name: 'Pricing',
    component: () => import('../views/Pricing.vue'),
    meta: {
      title: '价格 - FrontierX',
      requiresAuth: false
    }
  },
  {
    path: '/vocabulary-challenge/:id',
    name: 'VocabularyChallenge',
    component: VocabularyChallenge,
    meta: {
      requiresAuth: true
    }
  }
];

// 定义主页内的基础路由
const homeBaseRoutes = [
  {
    path: '',
    redirect: '/home/reading'
  },
  {
    path: 'reading',
    name: 'Reading',
    component: () => import('../views/Reading/index.vue')
  },
  {
    path: 'listening',
    name: 'Listening',
    component: () => import('../views/Listening.vue')
  },
  {
    path: 'speaking',
    name: 'Speaking',
    component: () => import('../views/Speaking.vue')
  },
  {
    path: 'writing',
    name: 'Writing',
    component: () => import('../views/Writing.vue')
  },
  {
    path: 'ai-friend',
    name: 'AIFriend',
    component: () => import('../views/AIFriend.vue')
  },
  {
    path: 'method',
    name: 'Method',
    component: () => import('../views/Method.vue')
  },
  {
    path: 'analysis',
    name: 'Analysis',
    component: () => import('../views/Analysis.vue')
  },
  {
    path: 'membership',
    name: 'Membership',
    component: () => import('../views/Membership.vue')
  },
  {
    path: 'settings',
    name: 'Settings',
    component: () => import('@/views/settings/index.vue')
  },
  {
    path: 'profile',
    name: 'Profile',
    component: () => import('../views/Profile.vue')
  }
];

// 定义阅读相关路由
const readingRoutes = [
  {
    path: 'reading/vocabulary/:id',
    name: 'ReadingVocabulary',
    component: () => import('../views/Reading/Vocabulary/index.vue'),
    meta: {
      requiresAuth: true,
      title: '阅读单词学习'
    }
  },
  {
    path: 'reading/essay/:id',
    name: 'ReadingEssay',
    component: () => import('../views/Reading/Essay/index.vue'),
    meta: {
      requiresAuth: true,
      title: '阅读文章'
    }
  }
];

// 定义听力相关路由
const listeningRoutes = [
  {
    path: 'listening/vocabulary/:id',
    name: 'ListeningVocabulary',
    component: () => import('../views/Listening/Vocabulary/index.vue'),
    meta: {
      requiresAuth: true,
      title: '听力单词学习'
    }
  },
  {
    path: 'listening/essay/:id',
    name: 'ListeningEssay',
    component: () => import('../views/Listening/Essay/index.vue'),
    meta: {
      requiresAuth: true,
      title: '听力文章'
    }
  },
  {
    path: 'listening/quiz/:id',
    name: 'ListeningQuiz',
    component: () => import('../views/ListeningQuiz/index.vue'),
    meta: {
      requiresAuth: true,
      title: '听力测试'
    }
  }
];

// 定义口语相关路由
const speakingRoutes = [
  {
    path: 'speaking/vocabulary/:id',
    name: 'SpeakingVocabulary',
    component: () => import('../views/Speaking/Vocabulary/index.vue'),
    meta: {
      requiresAuth: true,
      title: '口语单词学习'
    }
  },
  {
    path: 'speaking/essay/:id',
    name: 'SpeakingEssay',
    component: () => import('../views/Speaking/Essay/index.vue'),
    meta: {
      requiresAuth: true,
      title: '口语文章'
    }
  }
];

// 定义写作相关路由
const writingRoutes = [
  {
    path: 'writing/vocabulary/:id',
    name: 'WritingVocabulary',
    component: () => import('../views/Writing/Vocabulary/index.vue'),
    meta: {
      requiresAuth: true,
      title: '写作单词学习'
    }
  },
  {
    path: 'writing/essay/:id',
    name: 'WritingEssay',
    component: () => import('../views/Writing/Essay/index.vue'),
    meta: {
      requiresAuth: true,
      title: '写作文章'
    }
  }
];

// 定义重定向路由
const redirectRoutes = [
  {
    path: '/reading/vocabulary/:id',
    redirect: to => `/home/reading/vocabulary/${to.params.id}`
  },
  {
    path: '/reading/essay/:id',
    redirect: to => `/home/reading/essay/${to.params.id}`
  },
  {
    path: '/listening/vocabulary/:id',
    redirect: to => `/home/listening/vocabulary/${to.params.id}`
  },
  {
    path: '/listening/essay/:id',
    redirect: to => `/home/listening/essay/${to.params.id}`
  },
  {
    path: '/listening/quiz/:id',
    redirect: to => `/home/listening/quiz/${to.params.id}`
  },
  {
    path: '/speaking/vocabulary/:id',
    redirect: to => `/home/speaking/vocabulary/${to.params.id}`
  },
  {
    path: '/speaking/essay/:id',
    redirect: to => `/home/speaking/essay/${to.params.id}`
  },
  {
    path: '/writing/vocabulary/:id',
    redirect: to => `/home/writing/vocabulary/${to.params.id}`
  },
  {
    path: '/writing/essay/:id',
    redirect: to => `/home/writing/essay/${to.params.id}`
  }
];

// 组合所有主页内的路由
const homeChildrenRoutes = [
  ...homeBaseRoutes,
  ...readingRoutes,
  ...listeningRoutes,
  ...speakingRoutes,
  ...writingRoutes
];

// 定义主页路由
const homeRoute = {
  path: '/home',
  name: 'Home',
  component: () => import('../views/Home.vue'),
  children: homeChildrenRoutes
};

// 组合所有路由
const routes = [
  ...baseRoutes,
  homeRoute,
  ...redirectRoutes
];

// 创建路由器
const router = createRouter({
  history: createWebHistory(),
  routes
});

export default router;
